<template>
	<view>
		<!-- <view class="topHeader">
			
		</view> -->
		<!-- #ifdef MP -->
		<button>1234</button>
		<!-- #endif -->
		
		<u--form labelPosition="left" :model="model1" :rules="rules" ref="form1">

			<u-form-item label="昵称" prop="userInfo.nickname" borderBottom ref="item1">
				<u--input v-model="model1.userInfo.nickname" border="none"></u--input>
			</u-form-item>

			<u-form-item disabled label="用户名" prop="userInfo.username" borderBottom ref="item1">
				<u--input v-model="model1.userInfo.username" border="none"></u--input>
			</u-form-item>
		</u--form>
		<button @click="submit">确认保存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSex: false,
				model1: {
					userInfo: {
						nickname: '',
						username: '',
						password:''
					},
				},
				rules: {
					'userInfo.nickname': {
						type: 'string',
						required: true,
						message: '请填写昵称',
						trigger: ['blur', 'change']
					},
					'userInfo.username': {
						type: 'string',
						required: true,
						message: '请填写用户名',
						trigger: ['blur', 'change']
					},
				},
				radio: '',
				switchVal: false
			}
		},
		methods: {
			//根据用户名查询用户信息
			async getUserByid() {
				
				const userName = JSON.parse(localStorage.getItem("user")).userName
				console.log(userName)
				const res = await this.$api.getUserDetail({
					userName
				})
				console.log(res)
				if (res.code == 200) {
					localStorage.removeItem('user')
					localStorage.setItem('user', JSON.stringify(res.data.records[0]))
				}
				// console.log(res.data.records[0])
			},
			//用户修改信息
			userEdit:async function(userName,nickName){
				let id=JSON.parse(localStorage.getItem('user')).id
				let passWord=JSON.parse(localStorage.getItem('user')).passWord
				console.log(JSON.parse(localStorage.getItem('user')),'09876')
				let obj={
					id,
					userName,
					nickName
				}
				console.log(obj)
				const res = await this.$api.userEdit(obj)
				console.log(res)
				if(res.code==200){
					this.$tool.singleform()
					setTimeout(function(){
						uni.navigateBack({
								delta:1,//返回层数，2则上上页
							})
					},500)
					this.getUserByid()
				}else{
					alert('修改信息失败')
				}
			},
			//获取用户信息
			getUserDetail() {
				let userDetail = JSON.parse(localStorage.getItem('user'))
				let {
					userName,
					nickName
				} = userDetail
				this.model1.userInfo.nickname = nickName
				this.model1.userInfo.username = userName
			},
			sexSelect(e) {
				this.model1.userInfo.sex = e.name
				this.$refs.form1.validateField('userInfo.sex')
			},
			submit() {
				
				console.log(this.model1.userInfo);
				let {nickname,username}=this.model1.userInfo
				this.userEdit(username,nickname)
				// this.$tool.singleform()
				// setTimeout(function(){
				// 	uni.navigateBack({
				// 			delta:1,//返回层数，2则上上页
				// 		})
				// },500)
			}
		},
		onLoad() {
			this.getUserDetail()
		}
	}
</script>

<style lang="scss" scoped>
	.topHeader {
		height: 400rpx;
		background-color: antiquewhite;
	}

	uni-view {
		padding: 15rpx;
	}

	button {
		background-color: rgb(118,237,214);
		color: white;
		margin: 50rpx;
	}
</style>
